package browser

import (
	"bytes"
	"errors"
	"github.com/gogf/gf/os/gfile"
	"github.com/gogf/gf/text/gstr"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/util/guid"
	"io"
	"log"
	"mime/multipart"
	"net/http"
	"os"
	"path/filepath"
	"regexp"
)

func (this *Browser) DelCache() {
	os.RemoveAll(os.TempDir() + `\rod\cache`)
}
func UploadFile(filename string, file string) error {
	url := "http://192.168.16.252:8199/file/upload"
	extraParams := map[string]string{
		"saveName": filename,
	}
	request, _ := NewfileUploadRequest(url, extraParams, "file", file) //Filedata文件名称
	res, _ := http.DefaultClient.Do(request)
	defer res.Body.Close()
	bt, _ := io.ReadAll(res.Body)
	log.Println("上传返回：", string(bt))
	if string(bt) == "1" {
		return nil
	}
	return errors.New("上传失败")
}
func UploadByte(filename string, body []byte) error {
	url := "http://192.168.16.252:8199/file/upload"
	extraParams := map[string]string{
		"saveName": filename,
	}
	request, _ := NewByteUploadRequest(url, extraParams, "file", body) //Filedata文件名称
	res, _ := http.DefaultClient.Do(request)
	defer res.Body.Close()
	bt, _ := io.ReadAll(res.Body)
	if string(bt) == "1" {
		return nil
	}
	return errors.New("上传失败")
}

func NewfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	for key, val := range params {
		_ = writer.WriteField(key, val)
	}
	formcontenttype := writer.FormDataContentType()
	part, err := writer.CreateFormFile(paramName, filepath.Base(path))
	if err != nil {
		return nil, err
	}
	_, err = io.Copy(part, file)

	err = writer.Close()
	if err != nil {
		return nil, err
	}
	//fmt.Println(body)
	request, err := http.NewRequest("POST", uri, body)
	request.Header.Set("Content-Type", formcontenttype)
	return request, err
}
func NewByteUploadRequest(uri string, params map[string]string, paramName string, path []byte) (*http.Request, error) {
	file := bytes.NewReader(path)

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	for key, val := range params {
		_ = writer.WriteField(key, val)
	}
	formcontenttype := writer.FormDataContentType()
	part, err := writer.CreateFormFile(paramName, guid.S())
	if err != nil {
		return nil, err
	}
	_, err = io.Copy(part, file)

	err = writer.Close()
	if err != nil {
		return nil, err
	}
	//fmt.Println(body)
	request, err := http.NewRequest("POST", uri, body)
	request.Header.Set("Content-Type", formcontenttype)
	return request, err
}
func MoveFile(filename, toname string, id int) error {
	// 获取源文件信息
	filelist, _ := gfile.ScanDirFile(`D:\downpdf\`, "*", true)
	for _, v := range filelist {
		sv := GetStr(v)
		if gstr.Contains(sv, GetStr(filename)) {
			log.Println("开始移动文件", v)

			err := UploadFile(toname+"/"+gconv.String(id)+gfile.Ext(v), v)

			if err == nil {
				gfile.Remove(v)
				return nil
			} else {
				//更改文件名称
				gfile.Rename(v, gconv.String(id)+gfile.Ext(v))
				return nil
			}
		}
	}
	return errors.New("找不到文件")
}
func GetStr(s string) string {
	re, _ := regexp.Compile(`[A-Za-z0-9]+`)
	ss := re.FindAllString(s, -1)
	news := ""
	for _, s2 := range ss {
		news = news + s2
	}
	return news
}
